package com.general.framework.zzyy.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  * 因为wait/notify是依赖于monitor对象存在的，而monitor又依赖于synchronized，所以wait/notify必须和synchronized一起使用。
 *  * 如果去掉synchronized，代码会抛出 `IllegalMonitorStateException` 异常，而且 notify 也不能实现程序的唤醒功能了。


     代码编写要求
 *  * `wait`和`notify`必须再同步块或者方法里面，且成对出现
 *  * 先`wait`再`notify`，顺序不能乱
 */
public class LockSupportDemo {
    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "--->come in");
            LockSupport.park();
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "--->被唤醒");
        }, "t1");
        t1.start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "--->发出通知");
            LockSupport.unpark(t1);
        },"t2").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "--->发出通知");
            LockSupport.unpark(t1);
        },"t3").start();
    }
}
